package com.zucc.controller;

import com.zucc.controller.req.AttachmentSearchReq;
import com.zucc.controller.resp.AttachmentResp;
import com.zucc.service.AttachmentService;
import com.zucc.service.base.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-05 1:17 下午
 */
@RestController
@Slf4j
@RequestMapping("/v1/api/attachments")
@Api(tags = "附件管理")
public class AttachmentController {

    @Resource
    private AttachmentService attachmentService;

    @ApiOperation(value = "附件分页")
    @PostMapping("page")
    PageData<AttachmentResp> page(@RequestBody @Validated AttachmentSearchReq req) {
        return attachmentService.page(req);
    }

    @ApiOperation(value = "上传附件(上传资源等)")
    @PostMapping("/typeDictItem/{id}/buz/{buzId}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "附件类型字典项编号 （不同的业务附件）", dataType = "Long", example = "1", required = true),
            @ApiImplicitParam(paramType = "path", name = "buzId", value = "业务编号", dataType = "Long", example = "1", required = true)
    })
    public AttachmentResp upload(@PathVariable Long id, @PathVariable Long buzId, @RequestParam("file") MultipartFile file) {
        return attachmentService.upload(id, buzId, file);
    }

    @ApiOperation(value = "下载附件(下载资源等)")
    @GetMapping("/{id}")
    @ApiImplicitParam(paramType = "path", name = "id", value = "附件编号", dataType = "Long", example = "1", required = true)
    public void download(@PathVariable Long id, HttpServletResponse resp) {
        attachmentService.download(id, resp);
    }

    @ApiOperation(value = "下载 设备导入模板.xlsx")
    @GetMapping("/download/deviceExcel")
    public void downloadDeviceExcel(HttpServletResponse resp) {
        attachmentService.downloadFile(resp, "device.xlsx", "设备导入模板.xlsx");
    }
}
